import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  OneToMany,
} from 'typeorm';
import { Node } from './node.entity';
import { User } from '../../auth/entities/user.entity';
import { Reply } from './reply.entity';

@Entity()
export class Post {
  @PrimaryGeneratedColumn({
    type: 'int',
    name: 'id',
    comment: '帖子ID',
  })
  id: number;

  @Column({
    type: 'varchar',
    length: 100,
    comment: '帖子标题',
  })
  title: string;

  @Column({
    type: 'text',
    comment: '帖子内容',
  })
  content: string;

  @Column({
    type: 'int',
    default: 0,
    comment: '浏览次数',
  })
  views: number;

  @ManyToOne(() => Node, (node) => node.posts)
  node: Node;

  @ManyToOne(() => User)
  author: User;

  @OneToMany(() => Reply, (reply) => reply.post)
  replies: Reply[];

  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间',
  })
  createTime: Date;

  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
    comment: '更新时间',
  })
  updateTime: Date;
}
